<?php
/**
 * Created by PhpStorm.
 * User: 小明 <gongm@tongwei.com>
 * Date: 2017-02-09
 * Time: 15:08
 */

namespace App\Services\Portal;

use Illuminate\Support\Facades\DB;
use App\Core\Utils\TwHelper\Environment;
use App\Models\Portal\DiscuzArticleModel;


class ArticleService
{

    /**
     * 得到推荐文章列表
     * @return \Illuminate\Support\Collection
     */
    static public function getRecommendArticles($page = 1, $pageSize = 20)
    {
        $select_sql = "SELECT
                    a.aid,
                    a.title,
                    a.summary,
                    a.type,
                    a.uid,
                    a.username,
                    a.pic,
                    a.dateline,
                    a.url,
                    a.id,
                    a.idtype,
                    a.txhid,
                    a.txhname,
                    a.flag,
                    a.sort,
                    c.catid,
                    c.catname,
                    t.viewnum,
                    t.commentnum
                FROM
                    tw_portal_article_title a
                INNER JOIN tw_portal_category AS c ON a.catid = c.catid
                INNER JOIN tw_portal_article_count AS t ON a.aid = t.aid
                WHERE
                    a.status = 0
                AND a.txharticlestatus = 0
                AND a.istoindex <> 1
                ORDER BY
                    a.sort DESC,
                    a.dateline DESC
                LIMIT ?, ?";

        $limit_start = (($page - 1) * $pageSize);
        $limit_end = $pageSize;
        $articles = DB::connection('discuz')
            ->select($select_sql, [$limit_start, $limit_end]);

        // 处理日期时间
        $i = 0;
        foreach ($articles as $article)
        {
            $dateline = $article['dateline'];
            $articles[$i]['dateline'] = Environment::makeDate($dateline);

            $i = $i + 1;
        }

        return $articles;
    }

    /**
     *  获取文章 Article 列表。catid 默认为1 ，即获水产类全部文章
     *
     * @author gongm
     * @data 2018年2月24日
     */
    static public function getArticles($categoryID = 1, $pageSize = 20, $page = 1)
    {
        $select_sql = "SELECT
                    a.aid,
                    a.title,
                    a.summary,
                    a.type,
                    a.uid,
                    a.username,
                    a.pic,
                    a.dateline,
                    a.url,
                    a.id,
                    a.idtype,
                    a.txhid,
                    a.txhname,
                    a.flag,
                    a.sort,
                    c.catid,
                    c.catname,
                    t.viewnum,
                    t.commentnum
                FROM
                    tw_portal_article_title a
                INNER JOIN tw_portal_category AS c ON a.catid = c.catid
                INNER JOIN tw_portal_article_count AS t ON a.aid = t.aid
                WHERE
	            a.catid = ?
	            AND a.type = 1
	            AND a.status = 0	            
                AND a.txharticlestatus = 0
                AND a.istoindex <> 1
                ORDER BY
                    a.dateline DESC
                LIMIT ?, ?";

        $limit_start = (($page - 1) * $pageSize);
        $limit_end = $pageSize;
        $articles = DB::connection('discuz')->select($select_sql, [$categoryID, $limit_start, $limit_end]);

        // 处理日期时间
        $i = 0;
        foreach ($articles as $article)
        {
            $dateline = $article['dateline'];
            $articles[$i]['dateline'] = Environment::makeDate($dateline);

            $i = $i + 1;
        }

        return $articles;
    }
    /*static public function getArticles($catid = 1, $pageSize = 20, $page = 1)
    {
        $fields = [
            DiscuzArticleModel::FIELD_AID,
            DiscuzArticleModel::FIELD_TITLE,
            DiscuzArticleModel::FIELD_CATID,
            DiscuzArticleModel::FIELD_TYPE,
            DiscuzArticleModel::FIELD_SUMMARY,
            DiscuzArticleModel::FIELD_PIC,
            DiscuzArticleModel::FIELD_DATELINE
        ];
        $wheres = [
            [DiscuzArticleModel::FIELD_CATID, $catid],
            [DiscuzArticleModel::FIELD_STATUS, 0],
            [DiscuzArticleModel::FIELD_TYPE, 1]
        ];

        $articles = DiscuzArticleModel::select($fields)
            ->where($wheres)
            ->orderBy(DiscuzArticleModel::FIELD_DATELINE, 'DESC')
            ->paginate($pageSize);

        return $articles;
    }*/

}